package team.first.movieapiv2pro.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.alibaba.fastjson.JSON;
import com.mongodb.BasicDBObject;
import com.mongodb.DBCursor;
import com.mongodb.DBObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;
import team.first.movieapiv2pro.controller.StaffController;
import team.first.movieapiv2pro.dao.StaffDAO;
import team.first.movieapiv2pro.domain.Staff;
import team.first.movieapiv2pro.dto.RequestStaff;
import team.first.movieapiv2pro.service.StaffService;

import java.time.Duration;
import java.util.List;
import java.util.regex.Pattern;

/**
 * @Author: HAPPY
 * @Project_name: project
 * @Package_name: team.first.movieapiv2pro.service.impl
 * @Date: 2021/12/26 10:43
 * @Description:
 */
@Service
public class StaffServiceImpl implements StaffService {
    @Autowired
    StaffDAO staffDAO;
    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Override
    public Staff getById(Integer staffId) {
        ValueOperations<String, String> operations = stringRedisTemplate.opsForValue();

        String staffCache = operations.get("movie-api-v2:data:staff:" + staffId);

        Staff staff = null;

        if (staffCache != null) {
            staff = JSON.parseObject(staffCache, Staff.class);
        } else {
            staff = staffDAO.selectById(staffId);
            // 做缓存
            operations.set("movie-api-v2:data:staff:" + staffId, JSON.toJSONString(staff), Duration.ofSeconds(60 * 10));
        }

        return staff;
    }

    @Override
    public List<Staff> getStaff(RequestStaff requestStaff) {
        // 建立查询条件
        BasicDBObject filter_dbobject = new BasicDBObject();
        if (requestStaff.getChineseName() != null) {
            filter_dbobject.put("chineseName", Pattern.compile("^.*" + requestStaff.getChineseName() + ".*$", Pattern.CASE_INSENSITIVE));
        }
        if (requestStaff.getEnglishName() != null) {
            filter_dbobject.put("englishName", Pattern.compile("^.*" + requestStaff.getEnglishName() + ".*$", Pattern.CASE_INSENSITIVE));
        }

        if (requestStaff.getGender() != null) {
            filter_dbobject.put("gender", requestStaff.getGender());
        }

        if (requestStaff.getRegion() != null) {
            filter_dbobject.put("region", Pattern.compile("^.*" + requestStaff.getRegion() + ".*$", Pattern.CASE_INSENSITIVE));
        }
        //下面执行查询，设置limit,只要10条数据,排序(类mysql orderby) 再建一个BasicDBObject即可，－1表示倒序
        DBCursor cursor = staffDAO.selectStaff(filter_dbobject);

        StaffController.totalRow = cursor.count();
        if (requestStaff.getPagerSize() != null) {
            cursor.limit(requestStaff.getPagerSize());
        }
        if (requestStaff.getPageNumber() != null) {
            cursor.skip((requestStaff.getPageNumber() - 1) * requestStaff.getPagerSize());
        }
        if (requestStaff.getOrderField() != null) {
            // 根据排序规则排序，默认升序
            if (requestStaff.getOrderDirection() != null) {
                cursor.sort(new BasicDBObject(requestStaff.getOrderField(), "ASC".equalsIgnoreCase(requestStaff.getOrderDirection()) ? 1 : -1));
            } else {
                cursor.sort(new BasicDBObject(requestStaff.getOrderField(), 1));
            }
        }
        List<DBObject> dbObjects = cursor.toArray();
        return BeanUtil.copyToList(dbObjects, Staff.class);
    }
}
